package com.xy.smartplatfrom.controller;

import com.xy.smartplatfrom.dto.R;
import com.xy.smartplatfrom.pojo.Category;
import com.xy.smartplatfrom.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName:smartplatfrom
 * author:xiaoyang
 * time:2022/1/2 20:02
 * description:
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

     private CategoryService categoryService;

     @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/add")
    public R addCategory(@RequestBody Category category){
     categoryService.addCategory(category);
     return  R.setOK();
    }
    @GetMapping("/findAll")
    public R getAllCategories (){
        List<Category> categoryList = categoryService.selectAllCategory();
        return  R.setOK(categoryList);
    }
    @GetMapping("/findId/{id}")
    public R getCategoryById (@PathVariable Long id){
        Category category = categoryService.selectById(id);
        return  R.setOK(category);
    }
    @GetMapping("/findLike")
    public R getAllCategories (String cName){
        List<Category> categoryList = categoryService.selectCategoryByNameLike(cName);
        return  R.setOK(categoryList);
    }
    @DeleteMapping("/delete")
    public  R deleteCategory(@RequestBody List<Long> ids){
        int result = categoryService.deleteCategoryByIds(ids);
        return R.setOK();
    }







}
